home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / EDIT_UTL / EDITEXPT / UEXPERT.PAS < prev   
Pascal/Delphi Source File  |  1996-09-02  |  16KB  |  332 lines

  1. {*******************************************************************
  2.  *  Copyright (c) 1996 Valentin Sanchez Izquierdo                  *
  3.  *  CopyRight (c) 1996 ValClip Group                               *
  4.  *  This Expert is FreeWare,use on your risk                       *
  5.  *  Email: valemari@ctv.es                                         *
  6.  *  http://ourworld.compuserve.com/homepages/VSanchez              *
  7.  *  MODULO: IDE Editor Utility Expert                              *
  8.  *                                                                 *
  9.  *  DATE : 25/08/96 3:47:51                                        *
  10.  *                                                                 *
  11.  *  NOTE : Special Tanks from John Howe 100065.55@compuserve.com   *
  12.  *         for memSeparatorClick function.                         *
  13.  *                                                                 *
  14.  *  HISTORI:                                                       *
  15.  *                                                                 *
  16.  *                                                                 *
  17.  *******************************************************************}
  18. {
  19. My Terms
  20.  
  21. This product has been released into the public domain, FREE! 
  22. I want no money in return, and you are welcome to make as many copies, 
  23. and distribute them, as you like. However, I would appreciate some 
  24. feedback, especially bugs or imporovements that you think that I 
  25. should make. 
  26. }
  27.  
  28.  
  29. unit UExpert;
  30.  
  31. interface
  32.  
  33. uses Windows, Forms,Controls, Menus, Dialogs, Classes, ToolIntf, EditIntf, ExptIntf, VirtIntf, SysUtils,
  34.      Messages, Graphics, StdCtrls;
  35.  
  36. type 
  37. {TEditHeaderExpert}
  38.   TEditHeaderExpert = class(TIExpert)
  39.     menEditExp:     TIMenuItemIntf;                    // Menu Items
  40.     menEditSep:     TIMenuItemIntf;
  41.     menSeparator:   TIMenuItemIntf;
  42.     constructor     Create; virtual;
  43.     destructor      Destroy; override;
  44.     function        GetName: string; override;
  45.     function        GetStyle: TExpertStyle; override;
  46.     function        GetState: TExpertState; override;
  47.     function        GetIDString: string; override;
  48.     procedure       memHeaderClick(Sender: TIMenuItemIntf);
  49.     procedure       memSeparatorClick(Senfer: TIMenuItemIntf );
  50.     procedure       Execute; override;
  51.   end;
  52.  
  53. function InitExpert(ToolServices: TIToolServices; Registerproc: TExpertRegisterproc; var Terminate: TExpertTerminateProc): Boolean; export; stdcall;
  54. procedure Register;
  55.  
  56. var 
  57.     expEdit:     TEditHeaderExpert = NIL;
  58.  
  59. implementation
  60. {TEditHeaderExpert}
  61. //----------------------------------------------------------------------//
  62. // Constructor and menu creation
  63. constructor TEditHeaderExpert.Create;
  64. var         menMain:        TIMainMenuIntf;
  65.             menViewWinList: TIMenuItemIntf;
  66.             menViewMenu:    TIMenuItemIntf;
  67. begin
  68.   inherited Create;
  69.   menMain:=ToolServices.GetMainMenu;
  70.   if Assigned(menMain) then
  71.   try
  72.    {Find parent menu }
  73.     menViewWinList:=menMain.FindMenuItem('EditObjectItem');
  74.     if Assigned(menViewWinList) then
  75.     try
  76.       menViewMenu:=menViewWinList.GetParent;
  77.       if Assigned(menViewMenu) then
  78.       try
  79.         // You customize the menu display Text in your idiom.
  80.         // in English: Insert Header
  81.         //             Insert Separator
  82.         // see ^^^ Lines
  83.         menEditSep:=menViewMenu.InsertItem(menViewWinList.GetIndex+1, '-', 'EditExpSepa1', '', 0, 0, 0, [mfVisible], NIL);
  84.         menEditExp:=menViewMenu.InsertItem(menEditSep.GetIndex+1, 'I&nsertar Cabecera', 
  85.                                                                  //^^^^^^^^^^^^^^^^^^^ 
  86.                                 'EditInsertarCabeceraItem', '', ShortCut(Ord('C'), [ssCtrl, ssAlt]), 0, 0, [mfEnabled, mfVisible], memHeaderClick);
  87.         menSeparator:=menViewMenu.InsertItem(menEditExp.GetIndex+1, 'Insertar &Separador',
  88.                                                                      //^^^^^^^^^^^^^^^^^
  89.                                 'EditSeparatorItem','', ShortCut( Ord('S'),[ssCtrl, ssAlt]), 0, 0, [mfEnabled, mfVisible], memSeparatorClick);                                
  90.       finally
  91.         menViewMenu.Free;
  92.       end;
  93.     finally
  94.       menViewWinList.Free;
  95.     end;
  96.   finally
  97.     menMain.Free;
  98.   end;
  99. end;
  100.  
  101. //----------------------------------------------------------------------//
  102. destructor  TEditHeaderExpert.Destroy;
  103. begin
  104.   menEditExp.Free;                    // Release memory
  105.   menEditSep.Free;
  106.   menSeparator.Free;  
  107. inherited Destroy;
  108. end;
  109.  
  110. //----------------------------------------------------------------------//
  111. // Header insertion
  112. procedure   TEditHeaderExpert.memHeaderClick(Sender: TIMenuItemIntf);
  113. const
  114.      cr = chr(13)+chr(10);
  115. var
  116.    mModule: TIModuleInterface;
  117.    mEdit: TIEditorInterface;
  118.    writer: TIEditWriter;
  119.    sActual: string;
  120.    sz: PChar;
  121.    HW: HWND;
  122. begin
  123.     If Toolservices = nil then exit;                                   // No toolservices, no can do
  124.     HW := FindWindow('TeditWindow',Nil);             {Evitamos que no este abierto el editor}
  125.     if HW > 0 then begin
  126.     try
  127.       GetMem(sz,255);
  128.       sActual := ToolServices.GetCurrentFile;                         {Fichero actual en edicion}
  129.       mModule := ToolServices.GetModuleInterface( sActual );           {cojemos un interface para el modulo}
  130.       mEdit := mModule.GetEditorInterface;                            {Cojemos uninterface para el editor}
  131.       writer := mEdit.CreateWriter;                                   {por fin a escribir}
  132.       // You customice the text for you use
  133.       StrPCopy(sz,' *  FECHA: '+DateToStr( date)+ ' '+TimeToStr(Time)+'                                        *'+cr);
  134.       writer.Insert('{*******************************************************************'+cr);
  135.       writer.Insert(' *  Copyright (c) 1996 Valentin Sanchez Izquierdo                  *'+cr);
  136.       writer.Insert(' *  CopyRight (c) 1996 ValClip Group                               *'+cr);
  137.       writer.Insert(' *                                                                 *'+cr);
  138.       writer.Insert(' *  MODULO:                                                        *'+cr);
  139.       writer.Insert(' *                                                                 *'+cr);
  140.       writer.Insert(sz);
  141.       writer.Insert(' *                                                                 *'+cr);
  142.       writer.Insert(' *  NOTAS:                                                         *'+cr);
  143.       writer.Insert(' *                                                                 *'+cr);
  144.       writer.Insert(' *                                                                 *'+cr);
  145.       writer.Insert(' *  HISTORIA:                                                      *'+cr);
  146.       writer.Insert(' *                                                                 *'+cr);
  147.       writer.Insert(' *                                                                 *'+cr);
  148.       writer.Insert(' *******************************************************************}'+cr);
  149.     finally
  150.       writer.free;                                                    {Liberamos recursos}
  151.       mEdit.Free;
  152.       mModule.Free;
  153.       FreeMem(sz);
  154.     end;  
  155.    end; 
  156. end;
  157. //----------------------------------------------------------------------//
  158. // This function is created from John Howe 100065.55@compuserve.com 
  159. //----------------------------------------------------------------------//
  160. procedure  TEditHeaderExpert.memSeparatorClick(Senfer: TIMenuItemIntf );
  161.  
  162. Var
  163.   MoInt     : TIModuleInterface;                           // module interface
  164.   EdInt     : TIEditorInterface;                           // editor interface
  165.   EdView    : TIEditView;                                  // the editor view
  166.   EdWrit    : TiEditWriter;                                // Edit Writer
  167.   EdRead    : TIEditReader;                                // Edit Reader
  168.   Ep        : TEditPos;                                    // the editorposition
  169.   FName     : String;                                      // The File Name
  170.   Posn      : Integer;                                     // Position
  171.   Lf        : array[0..1] of Char;                         // Local Buffer
  172.   Lines     : Integer;                                     // Local Line Counter
  173.   EdPos     : Integer;                                     // Current Position in Editor Buffer
  174.   Eof       : Boolean;                                     // True if at end of Edit Buffer
  175.   FoundIt   : Boolean;                                     // True if Line Found
  176.  
  177.   Function ReadEditorLine : String;                        // Read a return a line from the Editor
  178.   Var
  179.     CharPos : Integer;                                     // Current Position in the result string
  180.   Begin
  181.     SetLength(Result,1024);                                // Longest Line !!!!
  182.     CharPos := 1;                                          // begin at the beginning
  183.     Repeat
  184.       Try
  185.         Eof := EdRead.GetText(EdPos,
  186.                               @Result[CharPos],1) <> 1;    // Read a character
  187.       Except                                               // Exception trap
  188.         Begin
  189.           Eof := True;                                     // Eof JIC
  190.           Result := '';                                    // No Result
  191.           end;
  192.         end;
  193.       Inc(EdPos);                                          // Read one so increment the buffer position
  194.       If Result[CharPos] = #10 then begin                  // was it a LineFeed and if so...
  195.         Result[CharPos] := #0;                             // Null the last char
  196.         SetLength(Result,CharPos - 1);                     // set the length
  197.         Exit;                                              // bye bye
  198.         end;
  199.       Until Eof;                                           // repeat till end of file
  200.     Result := '';                                          // if we get here we are eof so ''
  201.   end;
  202.  
  203.   Function GotoLine(LineNo : Integer) : Boolean;           // Goto line from file start
  204.   Var
  205.     Lines     : Integer;                                   // Line count
  206.   Begin
  207.     Result := False;                                       // assume failure
  208.     If LineNo = 1 then exit;                               // if first line we are there already
  209.     EdPos := 0;                                            // zap editor pos
  210.     Lines := 1;                                            // First line
  211.     While (NOT Result) do begin                            // while not done
  212.       Try
  213.         Eof := EdRead.GetText(EdPos,Lf,1) <> 1;            // read a character
  214.       Except
  215.         Eof := True;                                       // assume EOF on exception
  216.         end;
  217.       Inc(EdPos);                                          // Bump Editor position
  218.       If Lf[0] = #10 then Inc(Lines);                      // if EOL then bump count
  219.       If Lines = LineNo then Begin                         // if this is the line we want
  220.         Eof := False;                                      //  not EOF
  221.         Result := True;                                    // got it
  222.         end;
  223.       end;
  224.   End;
  225.  
  226. Begin
  227.   If Toolservices = nil then exit;                                   // No toolservices, no can do
  228.   Fname := Toolservices.GetCurrentFile;                              // Get the file name
  229.   If Pos('.DFM',UpperCase(FName)) > 0 then begin           // if this is a DFM
  230.     ShowMessage('Pascal source must have focus');          // Tell em not to be stupid
  231.     Exit;                                                  // Bye Bye
  232.     end;
  233.   if Not Toolservices.IsFileOpen(FName) then begin                   // if file is not open
  234.     If Not Toolservices.OpenFile(FName) then begin                   // try and open it
  235.       MessageDlg(Format('Can''t open %s',[FName]),
  236.                               mtError,[mbOk],0);           // tell em we failed
  237.       Exit;                                                // bye bye
  238.       end;
  239.     end;
  240.   MoInt := Toolservices.GetModuleInterface(Fname);                   // get the module interface for the file
  241.   Try
  242.     EdInt := MoInt.GetEditorInterface;                     // get the editor interface
  243.     Try
  244.       EdView := EdInt.GetView(0);                          // get the view
  245.       Try
  246.         Ep := EdView.GetPos(0);                            // get the current cursor position
  247.       Finally
  248.         EdView.Free;                                       // free the view
  249.         end;
  250.       EdRead := EdInt.CreateReader;                        // Create the Reader
  251.       Try
  252.         FoundIt := GotoLine(Ep.Line);                      // jump to the required line
  253.       Finally
  254.         EdRead.Free;                                       // Free the buffer
  255.         end;
  256.       If Not FoundIt then exit;                            // bomb out if not found
  257.       EdWrit := EdInt.CreateWriter;                        // get the vWriter
  258.       Try
  259.         With EdWrit Do Begin                               // with the writer
  260.           CopyTo(EdPos);                                   // move down to the first char of the line
  261.           Insert('//----------------------------------------------------------------------//'#13#10);                        // insert the debug stuff
  262.           end;
  263.       Finally
  264.         EdWrit.Free;                                       // free the view
  265.         end;
  266.     Finally
  267.       EdInt.Free;                                          // free the interface
  268.       end;
  269.   Finally
  270.     MoInt.Free;                                            // free the module
  271.     End;
  272. end;
  273.  
  274. //----------------------------------------------------------------------//
  275. procedure   TEditHeaderExpert.Execute;
  276. begin
  277.   {nada}
  278. end;
  279.  
  280. //----------------------------------------------------------------------//
  281. function    TEditHeaderExpert.GetName: String;
  282. begin
  283.   Result:='Editor Expert';
  284. end;
  285.  
  286. //----------------------------------------------------------------------//
  287. function    TEditHeaderExpert.GetStyle: TExpertStyle;
  288. begin
  289.   Result:=esAddIn;
  290. end;
  291.  
  292. //----------------------------------------------------------------------//
  293. function    TEditHeaderExpert.GetState: TExpertState;
  294. begin
  295.      Result := [esEnabled]
  296. end;
  297.  
  298. //----------------------------------------------------------------------//
  299. function    TEditHeaderExpert.GetIDString: String;
  300. begin
  301.   Result:='ValCLip.EditExpert';
  302. end;
  303.  
  304. //----------------------------------------------------------------------//
  305. //Codigo de Registro 
  306. procedure Register;
  307. begin
  308.   RegisterLibraryExpert(TEditHeaderExpert.Create);
  309. end;
  310.  
  311. //----------------------------------------------------------------------//
  312. procedure TerminateProc;
  313. begin
  314. end;
  315.  
  316. //----------------------------------------------------------------------//
  317. // Standar init
  318. function    InitExpert(ToolServices: TIToolServices; Registerproc: TExpertRegisterproc; var Terminate: TExpertTerminateProc): Boolean; export; stdcall;
  319. begin
  320.   Result:=ExptIntf.ToolServices=NIL;
  321.   if not Result then
  322.     exit;
  323.   ExptIntf.ToolServices:=ToolServices;
  324.   if Assigned(ToolServices) then
  325.     Application.Handle:=ToolServices.GetParentHandle;
  326.   Terminate:=NIL;
  327.   expEdit:=TEditHeaderExpert.Create;
  328.   RegisterProc(expEdit);
  329. end;
  330.  
  331. end.
  332.